<?php

namespace Alipay\AlipayFund;

/**
 * 支付功能
 * 1.isv批量支付，查询
 * 2.单笔支付，查询
 */

use Alipay\AlipayFund\Info\Parties;
use Alipay\AlipayFund\Info\PayeeList;
use Alipay\AlipayFund\Request\AlipayFundAccountQueryRequest;
use Alipay\AlipayFund\Request\AlipayFundBatchBillEreceiptApplyRequest;
use Alipay\AlipayFund\Request\AlipayFundBatchBillEreceiptQueryRequest;
use Alipay\AlipayFund\Request\AlipayFundBatchCloseRequest;
use Alipay\AlipayFund\Request\AlipayFundBatchCreateRequest;
use Alipay\AlipayFund\Request\AlipayFundBatchDetailRequest;
use Alipay\AlipayFund\Request\AlipayFundTransCommonQueryRequest;
use Alipay\AlipayFund\Request\AlipayFundTransPagePayRequest;
use Alipay\AlipayFund\Request\AlipayFundTransUniTransferRequest;
use Alipay\Common\CertClient;

class AlipayFund extends Builder
{
    public function __construct(array $config)
    {
        $this->appId = $config['APPID'] ?? '';

        $this->privateKey = $config['APP_PRIVATE_KEY'] ?? '';
        $this->privateKeyFile = $config['APP_PRIVATE_KEY_FILE'] ?? '';

        $this->alipayPublicKey = $config['ALIPAY_PUBLIC_KEY'] ?? '';

        $this->returnUrl = $config['RETURN_URL'] ?? '';

        $this->logsDir = $config['LOGS_DIR'] ?? '';

        $this->appPublicCert = $config['app_cert_path'] ?? '';

        $this->alipayPublicCert = $config['alipay_public_path'] ?? '';

        $this->alipayRootCert = $config['alipay_root_path'] ?? '';

        if ($this->alipayPublicCert and $this->alipayRootCert and $this->appPublicCert) {
            $this->certClient = new CertClient();
            $this->certClient->setAppPublicCert($this->appPublicCert)
                ->setAlipayPublicCert($this->alipayPublicCert)
                ->setAlipayRootCert($this->alipayRootCert);

            $this->isCertModel = $this->certClient->isCertModel;
        }

    }

    /**
     * 批次下单
     * @param Parties $parties
     * @param PayeeList $payeeList
     * @param string $batchNo
     * @param string $orderTitle
     * @param string $remark
     * @param string $expireTime
     * @param string $callBackParams
     * @return AlipayFundBatchCreateRequest
     * @author xis
     */
    public function batchCreate(Parties $parties, PayeeList $payeeList, string $batchNo, string $orderTitle, string $remark = '', string $expireTime = '', string $callBackParams = ''): AlipayFundBatchCreateRequest
    {
        $batchCreateRequest = new AlipayFundBatchCreateRequest();

        $batchCreateRequest->setParties($parties)
            ->setPayeeList($payeeList)
            ->setOutBatchNo($batchNo)
            ->setOrderTitle($orderTitle)
            ->setRemark($remark)
            ->setCallBackParams($callBackParams)
            ->setExpireTime($expireTime);

        return $batchCreateRequest;
    }

    /**
     * 批次支付
     * @param string $batchTransId
     * @return AlipayFundTransPagePayRequest
     * @author xis
     */
    public function transPay(string $batchTransId): AlipayFundTransPagePayRequest
    {
        $payRequest = new AlipayFundTransPagePayRequest();

        $payRequest->setOrderId($batchTransId);

        return $payRequest;
    }

    /**
     * 批次查询
     * @param string $outBatchNo
     * @return AlipayFundBatchDetailRequest
     * @author xis
     */
    public function batchQuery(string $outBatchNo): AlipayFundBatchDetailRequest
    {
        $detail = new AlipayFundBatchDetailRequest();
        $detail->setOutBatchNo($outBatchNo);
        return $detail;
    }

    /**
     * 批量订单关闭
     * @param string $batchTransId
     * @return AlipayFundBatchCloseRequest
     * @author xis
     */
    public function batchClose(string $batchTransId): AlipayFundBatchCloseRequest
    {
        $payRequest = new AlipayFundBatchCloseRequest();

        $payRequest->setOrderId($batchTransId);

        return $payRequest;
    }


    /**
     * 批量订单电子回单申请
     * @param string $batchTransId
     * @return AlipayFundBatchBillEreceiptApplyRequest
     * @author xis
     */
    public function batchEreceiptApply(string $batchTransId): AlipayFundBatchBillEreceiptApplyRequest
    {
        $request = new AlipayFundBatchBillEreceiptApplyRequest();
        $request->setKey($batchTransId);
        return $request;
    }

    /**
     * 批量订单查询电子回单状态
     * @param string $fileId
     * @return AlipayFundBatchBillEreceiptQueryRequest
     * @author xis
     */
    public function batchEreceiptQuery(string $fileId): AlipayFundBatchBillEreceiptQueryRequest
    {
        $request = new AlipayFundBatchBillEreceiptQueryRequest();
        $request->setFileId($fileId);
        return $request;
    }

    /**
     * 账户余额查询
     * @param $alipayUserId
     * @return AlipayFundAccountQueryRequest
     */
    public function accountQuery($alipayUserId): AlipayFundAccountQueryRequest
    {
        $request = new AlipayFundAccountQueryRequest();
        $request->setAlipayUserId($alipayUserId);
        return $request;
    }

    /**
     * 单笔转账
     * @param string $outBizNo
     * @param string $amount
     * @param Parties $party
     * @param string $orderTitle
     * @param string $remark
     * @return AlipayFundTransUniTransferRequest
     * @author xis
     */
    public function trans(string $outBizNo, string $amount, Parties $party, string $orderTitle, string $remark = ''): AlipayFundTransUniTransferRequest
    {
        $request = new AlipayFundTransUniTransferRequest();
        $request->setOrderTitle($orderTitle)
            ->setRemark($remark)
            ->setAmount($amount)
            ->setOutBizNo($outBizNo)
            ->setParty($party);

        return $request;
    }

    /**
     * 订单查询
     * @param string $outBizNo
     * @param string $orderId
     * @param string $payFundOrderId
     * @return AlipayFundTransCommonQueryRequest
     */
    public function transQuery(string $outBizNo = '', string $orderId = '', string $payFundOrderId = ''): AlipayFundTransCommonQueryRequest
    {
        $request = new AlipayFundTransCommonQueryRequest();
        $request->setOrderId($orderId);
        $request->setOutBizNo($outBizNo);
        $request->setPayFundOrderId($payFundOrderId);
        return $request;
    }
}